package com.zjut.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zjut.dao.PaperDao;
import com.zjut.domain.Paper;
import com.zjut.domain.Paper;
import com.zjut.service.impl.PaperServiceImpl;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PaperService implements PaperServiceImpl {
    @Autowired
    private PaperDao paperDao;

    @Override
    public boolean save(Paper paper) {
        return paperDao.insert(paper) > 0;
    }

    @Override
    public boolean update(Paper paper) {
        return paperDao.updateById(paper) > 0;
    }

    @Override
    public boolean delete(Integer id) {
        return paperDao.deleteById(id) > 0;
    }

    //通过ID查找
    @Override
    public Paper getById(Integer id) {
        return paperDao.selectById(id);
    }

    //查找全部
    @Override
    public List<Paper> getAll() {
        return paperDao.selectList(null);
    }

    @Override
    public IPage<Paper> getPage(Integer currentPage, Integer pageSize, Paper paper) {
        LambdaQueryWrapper<Paper> lqw = new LambdaQueryWrapper<>();
        lqw.like(Strings.isNotEmpty(paper.getName()), Paper::getName, paper.getName());
        lqw.eq(Strings.isNotEmpty(paper.getAuthor()), Paper::getAuthor, paper.getAuthor());

        IPage page = new Page(currentPage, pageSize);
        paperDao.selectPage(page, lqw);
        return page;
    }

    @Override
    public int savePaper(Paper paper) {
        paperDao.insert(paper);
        return paper.getId();
    }

}
